package com.esm.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.esm.model.User;
import com.esm.service.UserService;
import com.esm.serviceimpl.UserServiceImpl;
import com.esm.utils.PagingUtil;
import com.esm.utils.PrintWriterUtil;

import net.sf.json.JSONArray;

/**
 * SelectDeptEmpController
 * 选择部门下面员工控制者
 * @author 魏长均
 *
 */
public class SelectDeptEmpController extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	UserService uService = new UserServiceImpl();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
	{
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
	{
		HttpSession session = req.getSession();
		int rid = 7;
		int dep_id = 0;
		String page = req.getParameter("page");
		if(page == null)
		{
			page = "1";
			//根据部门名称获取部门id号
			dep_id = Integer.parseInt(req.getParameter("dep_id"));
			session.setAttribute("dep_id", dep_id);
		}
		else
		{
			//如果page != null:这说明用户点击了其他分页，那么dep_id将从前面存的session里面去获取
			dep_id = (Integer) session.getAttribute("dep_id");
		}
		JSONArray jao = uService.getDeptEmployees(rid, dep_id, Integer.parseInt(page), 4);
		int ucount  =uService.getDeptEmpolyeesCount(rid, dep_id);
		String sDempPageList = PagingUtil.getSelDepEmpPageList(ucount, Integer.parseInt(page), 4);
//		session.setAttribute("sDempPageList", sDempPageList);
		PrintWriter pw = resp.getWriter();
		jao.add(sDempPageList);
		PrintWriterUtil.flushJaoAndClosePrintWriter(pw, jao);
	}
}
